//
//  CYMessageCenterViewController.m
//  cyydt
//
//  Created by chu on 2019/1/21.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYMessageCenterViewController.h"

#import "CYMessageJingXuanViewController.h"
#import "CYMessageNotificationViewController.h"
#import "CYMessageNoticeViewController.h"


@interface CYMessageCenterViewController ()<SGTopScrollMenuDelegate, UIScrollViewDelegate>

{
    NSNumber *_inform;
    NSNumber *_choose;
    NSNumber *_notice;
    
    RKNotificationHub *_jingxuanHub;
    RKNotificationHub *_gonggaoHub;
}

@property (nonatomic, strong) SGTopScrollMenu *topMenu;

@property (nonatomic, strong) UIScrollView *mainScrollView;

@end

@implementation CYMessageCenterViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSArray *arr = self.topMenu.allTitleLabel;
    for (int i = 0; i < arr.count; i++) {
        if (i == 0) {
            UILabel *jingxuan = _topMenu.allTitleLabel[0];
            _jingxuanHub = [[RKNotificationHub alloc] initWithView:jingxuan];
            [_jingxuanHub scaleCircleSizeBy:0.5];
            [_jingxuanHub moveCircleByX:-(jingxuan.frame.size.width / 2 - 25) Y:5];
            [_jingxuanHub showCount];
            
            
        }
        if (i == 2) {
            UILabel *gongao = _topMenu.allTitleLabel[2];
            _gonggaoHub = [[RKNotificationHub alloc] initWithView:gongao];
            [_gonggaoHub scaleCircleSizeBy:0.5];
            [_gonggaoHub moveCircleByX:-(gongao.frame.size.width / 2 - 25) Y:5];
            [_gonggaoHub showCount];
        }
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getMessageCount) name:@"messageHaveRead" object:nil];
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"messageHaveRead" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的消息";
    _inform = 0;
    _choose = 0;
    _notice = 0;
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, 44, kWindowW, kWindowH - Height_NavBar - 44);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = NO;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    
    [self setChildViewControllers];
    
    [self.view addSubview:self.topMenu];
    
}

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    // 1 计算滚动的位置
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.mainScrollView.frame.size.height);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.bounds.size.width;
    // 1.添加子控制器view
    [self showVc:index];
    [self.topMenu selectLabel:self.topMenu.allTitleLabel[index]];
}

#pragma mark - SGTopScrollMenuDelegate
- (void)SGTopScrollMenu:(SGTopScrollMenu *)topScrollMenu didSelectTitleAtIndex:(NSInteger)index{
    [self showVc:index];
    
}

- (void)setChildViewControllers{
    CYMessageJingXuanViewController *commend = [[CYMessageJingXuanViewController alloc] init];
    commend.view.frame = CGRectMake(0, 0, kWindowW, self.mainScrollView.frame.size.height);
    [self.mainScrollView addSubview:commend.view];
    [self addChildViewController:commend];
    
    CYMessageNotificationViewController *info = [[CYMessageNotificationViewController alloc] init];
    [self addChildViewController:info];
    
    CYMessageNoticeViewController *video = [[CYMessageNoticeViewController alloc] init];
    [self addChildViewController:video];

}


- (void)getMessageCount{
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"message/count"];
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:nil ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"未读消息 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                NSDictionary *data = responseResult[@"data"];
                self->_inform = data[@"inform"];
                self->_choose = data[@"choose"];
                self->_notice = data[@"notice"];
                [self initBadge];
            }else{
                
            }
        }
    }];
}

- (void)initBadge{
    [_jingxuanHub incrementBy:[_inform intValue]];
    [_gonggaoHub incrementBy:[_notice intValue]];

}


- (SGTopScrollMenu *)topMenu{
    if (!_topMenu) {
        _topMenu = [SGTopScrollMenu topScrollMenuWithFrame:CGRectMake(0, 0, kWindowW , 44) NormalColor:RGBOF(0x999999) SelColor:RGBOF(0xF43636)];
        _topMenu.topScrollMenuDelegate = self;
        _topMenu.titlesArr = @[@"精选", @"通知", @"公告"];
        
        
    }
    return _topMenu;
}


@end
